package com.company.project.web;
import com.alibaba.fastjson.JSON;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.Image;
import com.company.project.model.News;
import com.company.project.service.impl.ImageServiceImpl;
import com.company.project.util.FileUploadUtil;
import com.company.project.util.PageUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.awt.font.ImageGraphicAttribute;
import java.io.File;
import java.util.List;

/**
* Created by CodeGenerator on 2018/10/08.
*/
@RestController
@RequestMapping("/image")
public class ImageController {
    @Resource
    private ImageServiceImpl imageService;

    @Resource
    private FileUploadUtil fileUploadUtil;

    @Value("${file.imguploadFolder}")
    private String imageUri;

    @PostMapping("/add")
    public Result add(Image image) {
        imageService.save(image);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        imageService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(Image image) {
        imageService.update(image);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Image image = imageService.findById(id);
        return ResultGenerator.genSuccessResult(image);
    }

    @GetMapping("/list")
    public Result list() {
        List<Image> list = imageService.findAll();
        return ResultGenerator.genSuccessResult(list);
    }

    //前台查询轮播图
    @GetMapping("/select/list")
    public List<Image> getImgeList(){
        return imageService.getImgeList();
    }

    //后台查询前台首页轮播图
    @GetMapping("/admin/select/list/{index}")
    public PageUtil<Image> adminGetImgeList(@PathVariable Integer index){
        return imageService.adminGetImageList(index);
    }

    //后台添加图片
    @PostMapping("/admin/add/image")
    public Result adminAddImage(@RequestParam String imge,MultipartFile file){
        Image imges = JSON.parseObject(imge,Image.class);
        if(file!=null){
            String s = fileUploadUtil.writeFile(file, "img");
            if (s == null){
                return ResultGenerator.getInternalServerErrot("图片上传错误");
            }
            imges.setImageLocation(s);
            boolean bo =imageService.adminAddImage(imges);
            if(bo){
                return ResultGenerator.genSuccessResult(s);
            }
        }else {
            boolean bo =imageService.adminAddImage(imges);
            if(bo){
                return ResultGenerator.genSuccessResult("保存成功");
            }
            return ResultGenerator.getInternalServerErrot("保存错误");
        }

        return ResultGenerator.getInternalServerErrot("保存错误");
    }

    //后台修改图片
    @PostMapping("/admin/update/image")
    public Result adminUpdateImage(@RequestParam String imge,MultipartFile file){
        Image imges = JSON.parseObject(imge,Image.class);
        if(file!=null){
            String s = fileUploadUtil.writeFile(file, "img");
            if (s == null){
                return ResultGenerator.getInternalServerErrot("图片修改错误");
            }
            imges.setImageLocation(s);
            boolean bo =imageService.adminUpdateImage(imges);
            if(bo){
                return ResultGenerator.genSuccessResult(s);
            }
        }else {
            boolean bo =imageService.adminUpdateImage(imges);
            if(bo){
                return ResultGenerator.genSuccessResult("修改成功");
            }
            return ResultGenerator.getInternalServerErrot("修改错误");
        }

        return ResultGenerator.getInternalServerErrot("修改错误");
    }

    //后台删除
    @PostMapping("/delect/image")
    public boolean adminDelectImage(@RequestBody Image image){
        String uri = image.getImageLocation();
        uri= uri.substring(uri.lastIndexOf("/")+1,uri.length());
        File file = new File(imageUri+uri);
        if(file.exists()){
            file.delete();
        }
        return imageService.adminDelectImage(image);
    }

}
